package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.vo.CategoryListitemVO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/categories")
@Api(tags = "02.类别管理模块")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    public CategoryController() {
        log.info("创建控制器：CategoryController");
    }

    @PostMapping("/add-new")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    String addNew(CategoryAddNewDTO categoryAddNewDTO){
        log.debug("开始处理【添加类别】的请求：{}", categoryAddNewDTO);
        categoryService.addNew(categoryAddNewDTO);
        return "添加类别成功";
    }

    @PostMapping("/{id:[0-9]+}/deleteById")
    @ApiOperation("根据id删除类别")
    @ApiOperationSupport(order = 200)
    String deleteById(@PathVariable Long id){
        log.debug("开始处理【删除类别】的请求：id={}", id);
        categoryService.deleteById(id);
        return "删除成功";
    }


    @ApiOperation("获取类别列表")
    @ApiOperationSupport(order = 300)
    @GetMapping("/list")
    public JsonResult<List<CategoryListitemVO>> list(Long parentId){
        List<CategoryListitemVO> categoryListitemVOS = categoryService.listData(parentId);
        return JsonResult.ok(categoryListitemVOS);
    }
}
